package com.ruoyi.dynamic.service.impl;

import com.ruoyi.association.mapper.CustomerCollectDynamicMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.dynamic.domain.Dynamic;
import com.ruoyi.dynamic.mapper.DynamicMapper;
import com.ruoyi.dynamic.service.IDynamicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 动态 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class DynamicServiceImpl implements IDynamicService
{
    private static final Logger log = LoggerFactory.getLogger(DynamicServiceImpl.class);

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private CustomerCollectDynamicMapper customerCollectDynamicMapper;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询动态列表
     * 
     * @param dynamic 动态信息
     * @return 动态信息集合信息
     */
    @Override
    public List<Dynamic> selectDynamicList(Dynamic dynamic)
    {
        return dynamicMapper.selectDynamicList(dynamic);
    }

    /**
     * 通过动态名查询动态
     * 
     * @param dynamicTitle 动态名
     * @return 动态对象信息
     */
    @Override
    public Dynamic selectDynamicByDynamicTitle(String dynamicTitle)
    {
        return dynamicMapper.selectDynamicByDynamicTitle(dynamicTitle);
    }

    /**
     * 通过动态ID查询动态
     * 
     * @param dynamicId 动态ID
     * @return 动态对象信息
     */
    @Override
    public Dynamic selectDynamicById(Long dynamicId)
    {
        return dynamicMapper.selectDynamicById(dynamicId);
    }

    /**
     * 新增保存动态信息
     * 
     * @param dynamic 动态信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDynamic(Dynamic dynamic)
    {
        // 新增动态信息
        int rows = dynamicMapper.insertDynamic(dynamic);
        return rows;
    }

    /**
     * 修改保存动态信息
     * 
     * @param dynamic 动态信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDynamic(Dynamic dynamic)
    {
        return dynamicMapper.updateDynamic(dynamic);
    }

    /**
     * 修改动态信息 喜欢人数+1
     *
     * @param dynamicId 动态信息
     * @return 结果
     */
    @Override
    @Transactional
    public void updateDynamicNumLikeAdd(Long dynamicId)
    {
        dynamicMapper.updateDynamicNumLikeAdd(dynamicId);
    }

    /**
     * 修改动态信息 喜欢人数-1
     *
     * @param dynamicId 动态信息
     * @return 结果
     */
    @Override
    @Transactional
    public void updateDynamicNumLikeReduce(Long dynamicId)
    {
        dynamicMapper.updateDynamicNumLikeReduce(dynamicId);
    }

    /**
     * 修改动态审核状态
     * 
     * @param dynamic 动态信息
     * @return 结果
     */
    @Override
    public int updateDynamicStatus(Dynamic dynamic)
    {
        return dynamicMapper.updateDynamic(dynamic);
    }

    /**
     * 修改动态上架状态
     *
     * @param dynamic 动态信息
     * @return 结果
     */
    @Override
    public int updateDynamicActive(Dynamic dynamic)
    {
        return dynamicMapper.updateDynamic(dynamic);
    }

    /**
     * 批量删除动态信息
     * 
     * @param dynamicIds 需要删除的动态ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDynamicByIds(Long[] dynamicIds) {
        for(int i = 0; i < dynamicIds.length; i++){
            // 删除客户的收藏
            customerCollectDynamicMapper.deleteCustomerDynamicByDynamicId(dynamicIds[i]);
            // 删除文件
            Dynamic dynamic = dynamicMapper.selectDynamicById(dynamicIds[i]);
            if(StringUtils.isNotEmpty(dynamic.getDynamicImage())){
                String imageStr = dynamic.getDynamicImage().toString();
                String[] pathList = imageStr.split(",");
                for (int j = 0; j < pathList.length; j++) {
                    FileUploadUtils.deleteFile(pathList[j].substring(pathList[j].indexOf(Constants.RESOURCE_PREFIX), pathList[j].length()));
                }
            }
            if(StringUtils.isNotEmpty(dynamic.getDynamicVideo())){
                String videoStr = dynamic.getDynamicVideo().toString();
                FileUploadUtils.deleteFile(videoStr.substring(videoStr.indexOf(Constants.RESOURCE_PREFIX), videoStr.length()));
            }
        }
        return dynamicMapper.deleteDynamicByIds(dynamicIds);
    }
}
